package com.science.slam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.science.slam.domain.User;
import com.science.slam.exception.BusinessException;
import com.science.slam.exception.ErrorCode;
import com.science.slam.mapper.UserMapper;
import com.science.slam.request.LoginRequestBody;
import com.science.slam.response.BasicResponse;
import com.science.slam.service.UserService;
import com.science.slam.utils.IDUtils;
import com.science.slam.utils.UserUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/user/login")
    public BasicResponse<User> userLogin(@RequestBody LoginRequestBody loginRequestBody,
                                         HttpServletRequest request) {
        String phoneNumber = loginRequestBody.getPhoneNumber();
        String passwd = loginRequestBody.getPasswd();
        if (phoneNumber == null || passwd == null)
            throw new BusinessException(ErrorCode.PARAMS_NULL);
        User user = userService.selectByPhoneNumber(phoneNumber);
        if (user == null)
            throw new BusinessException(ErrorCode.PHONE_ERROR);
        if (!user.getPasswd().equals(passwd))
            throw new BusinessException(ErrorCode.PARAMS_PASSWD_ERROR);
        User user1 = UserUtils.getSafeUser(user);
        request.getSession().setAttribute("LOGIN_USER", user1);
        return BasicResponse.success(user1);
    }

    @PostMapping("/user/register")
    public BasicResponse<User> userRegister(String phoneNumber, String passwd) throws IOException {
        if (phoneNumber == null || passwd == null)
            throw new BusinessException(ErrorCode.PARAMS_NULL);
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("phone_number", phoneNumber);
        User user = userMapper.selectOne(userQueryWrapper);
        if (user != null)
            throw new BusinessException(ErrorCode.USER_EXITS);
        String count = IDUtils.uniqueUserID();
        user = new User(count, phoneNumber, passwd);
        boolean save = userService.save(user);
        if (save)
            return BasicResponse.success(UserUtils.getSafeUser(user));
        throw new BusinessException(ErrorCode.REGISTER_FAILED);
    }

    @PatchMapping("/user/edit")
    public BasicResponse<Object> editUserMessage(String colName, Object newMessage, HttpServletRequest request) {
        User user = userService.getCurrentUser(request);
        if (user == null)
            throw new BusinessException(ErrorCode.PARAMS_NULL);
        String account = user.getAccount();
        Integer i = userService.editUserMessage(account, colName, newMessage);
        if (i != 1)
            throw new BusinessException(ErrorCode.EDIT_FAIL);
        return BasicResponse.success(null);
    }

    @PatchMapping("user/exit")
    public void userExit(HttpServletRequest request) {
        request.getSession().setAttribute("LOGIN_USER", null);
    }

    @GetMapping("/user/currentUser")
    public BasicResponse<User> getCurrentUser(HttpServletRequest request) {
        User currentUser = userService.getCurrentUser(request);
        if (currentUser == null)
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        return BasicResponse.success(currentUser);
    }
}
